package Message;


import com.special.ResideMenuDemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sirius_swu on 2017/3/6.
 */
public class ProductService implements IProductService {
    private Product[] products = {
            new Product(1, "仙人掌", 1, R.drawable.plant, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(2, "芦荟", 2, R.drawable.plant1, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(3, "米兰", 1, R.drawable.plant2, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(4, "银皇帝", 1, R.drawable.plant3, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(5, "巴西铁", 2, R.drawable.plant4, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(6, "苏铁", 1, R.drawable.plant5, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(7, "黑美人", 1, R.drawable.plant6, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android"),
            new Product(8, "绿宝石", 1, R.drawable.plant7, "你好！仙人掌今天浇水了么？", "网络类型：WCDMA(4G) 操作系统：Android")
    };

    private List<Product> ps = null;

    public ProductService() {
        ps = new ArrayList<Product>();
        for (Product p:products) {
            ps.add(p);
        }
        System.out.println("Service"+ps.size());
    }

    @Override
    public List<Product> getAll() {
        return this.ps;
    }

    @Override
    public List<Product> getByPager(int pageIndex, int pageSize) {
        System.out.println("ProductService"+pageIndex);
        int totalCount = products.length;
        int pageCount = 0;
        if (totalCount%pageSize == 0) {
            pageCount = totalCount/pageSize;
        } else {
            pageCount = totalCount/pageSize+1;
        }
//        if (pageIndex >= pageCount-1) {
//            pageIndex = pageCount-1;
//        }
        Object[] source = null;
        source = this.ps.toArray();
        List<Product> result = new ArrayList<Product>();
        if (pageIndex*pageSize<+totalCount) {
            for (int i = pageIndex * pageSize; i < pageSize * (pageIndex + 1); i++) {
                if (i == ps.size()) {
                    break;
                }
                result.add((Product) source[i]);
            }
        }
        System.out.println("result"+result);
        return result;
    }

    @Override
    public Product getById(int productId) {
        for (Product p:this.ps) {
            if (p.getId() == productId) {
                return p;
            }
        }
        return null;
    }

    @Override
    public List<Product> getBtName(String name) {
        List<Product> result = new ArrayList<Product>();
        for (Product p:this.ps) {
            if (p.getName().indexOf(name) != -1) {
                result.add(p);
            }
        }
        return result;
    }

    @Override
    public void Insert(Product product) {
        this.ps.add(product);
    }

    @Override
    public void Modify(Product product) {
        for (int i = 0; i < this.ps.size(); i++) {
            if (product.getId() == ps.get(i).getId()) {
                ps.set(i, product);
                break;
            }
        }
    }

    @Override
    public void Del(int id) {
        Product p = this.getById(id);
        if (p != null) {
            this.ps.remove(p);
        }
    }
}
